package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.LinkmanDAO;
import com.feeyin.radar.crm.dao.LinkmanUpdateHistoryDAO;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.Linkman;
import com.feeyin.radar.crm.model.QueryLinkman;
import com.feeyin.radar.crm.service.LinkmanService;
import com.feeyin.radar.sys.dao.CountryDAO;
import com.feeyin.radar.sys.dao.DataDictionaryDAO;
import com.feeyin.radar.sys.dao.RecordRangeDAO;
import com.feeyin.radar.sys.model.DataDictionary;
import com.feeyin.radar.sys.model.RecordRange;
import com.feeyin.radar.sys.service.DepartmentService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LinkmanServiceImpl
  implements LinkmanService
{
  private LinkmanDAO linkmanDAO;
  private DataDictionaryDAO dataDictionaryDAO;
  private RecordRangeDAO recordRangeDAO;
  private CountryDAO countryDAO;
  private CustomerDAO customerDAO;
  private LinkmanUpdateHistoryDAO linkmanUpdateHistoryDAO;
  private DepartmentService departmentService;

  public DepartmentService getDepartmentService()
  {
    return this.departmentService;
  }

  public void setDepartmentService(DepartmentService departmentService) {
    this.departmentService = departmentService;
  }

  public LinkmanDAO getLinkmanDAO() {
    return this.linkmanDAO;
  }

  public void setLinkmanDAO(LinkmanDAO linkmanDAO) {
    this.linkmanDAO = linkmanDAO;
  }

  public DataDictionaryDAO getDataDictionaryDAO() {
    return this.dataDictionaryDAO;
  }

  public void setDataDictionaryDAO(DataDictionaryDAO dataDictionaryDAO) {
    this.dataDictionaryDAO = dataDictionaryDAO;
  }

  public RecordRangeDAO getRecordRangeDAO() {
    return this.recordRangeDAO;
  }

  public void setRecordRangeDAO(RecordRangeDAO recordRangeDAO) {
    this.recordRangeDAO = recordRangeDAO;
  }

  public CountryDAO getCountryDAO() {
    return this.countryDAO;
  }

  public void setCountryDAO(CountryDAO countryDAO) {
    this.countryDAO = countryDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public LinkmanUpdateHistoryDAO getLinkmanUpdateHistoryDAO() {
    return this.linkmanUpdateHistoryDAO;
  }

  public void setLinkmanUpdateHistoryDAO(LinkmanUpdateHistoryDAO linkmanUpdateHistoryDAO)
  {
    this.linkmanUpdateHistoryDAO = linkmanUpdateHistoryDAO;
  }

  public Map getLinkmanInfoRecords() {
    Map map = new HashMap();

    List linkmanDictionarys = new ArrayList();
    linkmanDictionarys = this.dataDictionaryDAO.getDataDictionarysByRecordType("2");
    if (linkmanDictionarys != null) {
      for (int i = 0; i < linkmanDictionarys.size(); i++) {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = (DataDictionary)linkmanDictionarys.get(i);
        map.put(dataDictionary.getRecordCode(), dataDictionary);
      }
    }
    return map;
  }

  public Map getLinkmanDictionaryRanges() {
    return this.linkmanDAO.getLinkmanDictionaryRanges();
  }

  public Map getLinkmanAndDepts(Integer customerId) {
    if (customerId == null) return null;

    Map map = new HashMap();
    DataDictionary dataDictionary = new DataDictionary();
    dataDictionary = this.dataDictionaryDAO.getDataDictionaryByRecordCode("22");
    if (dataDictionary != null) {
      map.put("linkmanDepts", this.recordRangeDAO.getActivityRecordRangesByDictionaryId(dataDictionary.getId()));
    }
    List linkmans = new ArrayList();
    linkmans = this.linkmanDAO.getLinkmansByCustomerId(customerId);
    map.put("linkmans", linkmans);

    return map;
  }

  public Linkman getLinkman(Integer id) {
    if (id == null) return null;
    Linkman linkman = this.linkmanDAO.getLinkman(id);

    RecordRange recordRange = null;
    if (linkman != null) {
      if (linkman.getLinkmanDeptId() != null) {
        recordRange = new RecordRange();
        recordRange = this.recordRangeDAO.getRecordRangeById(linkman.getLinkmanDeptId());
        if (recordRange != null) {
          linkman.setLinkmanDept(recordRange.getRangeValue());
        }
      }
      if (linkman.getLinkmanDutyId() != null) {
        recordRange = new RecordRange();
        recordRange = this.recordRangeDAO.getRecordRangeById(linkman.getLinkmanDutyId());
        if (recordRange != null) {
          linkman.setLinkmanDuty(recordRange.getRangeValue());
        }
      }
    }

    return linkman;
  }

  public boolean insertLinkman(Linkman linkman) {
    if (linkman == null) return false;
    if (linkman.getCustomerId() == null) return false;
    linkman.setCreateDate(new Date());
    linkman.setLinkmanCode(linkman.getCustomerId() + DateUtils.getDateByFormat("yyyyMMddHHmmss"));
    linkman.setLinkmanDeptId(this.linkmanDAO.linkmanDeptId(linkman.getDeptDictionaryId(), linkman.getLinkmanDept()));
    linkman.setLinkmanDutyId(this.linkmanDAO.linkmanDutyId(linkman.getDutyDictionaryId(), linkman.getLinkmanDuty()));
    linkman.setStatus("0");
    boolean result = this.linkmanDAO.insertLinkman(linkman);

    Customer customer = new Customer();
    List linkmans = new ArrayList();
    linkmans = this.linkmanDAO.getLinkmansByCustomerId(linkman.getCustomerId());
    if (linkmans != null)
      customer.setLinkmanNum(Integer.valueOf(linkmans.size()));
    else {
      customer.setLinkmanNum(Integer.valueOf(0));
    }
    customer.setId(linkman.getCustomerId());
    this.customerDAO.updateCustomer(customer);

    Linkman linkman2 = new Linkman();
    linkman2 = this.linkmanDAO.getLinkmanByLinkmanCode(linkman.getLinkmanCode());
    if (linkman2 != null) {
      linkman2.setLinkmanId(linkman2.getId());
      linkman2.setUpdatePersonId(linkman.getCurrentUserId());
      linkman2.setUpdateDate(new Date());
      linkman2.setIsPrimitive("1");
      this.linkmanUpdateHistoryDAO.insertLinkmanUpdateHistory(linkman2);
    }

    return result;
  }

  public boolean updateLinkman(Linkman linkman) {
    if (linkman == null) return false;
    List list = null;
    RecordRange recordRange = null;
    linkman.setLinkmanDeptId(this.linkmanDAO.linkmanDeptId(linkman.getDeptDictionaryId(), linkman.getLinkmanDept()));
    linkman.setLinkmanDutyId(this.linkmanDAO.linkmanDutyId(linkman.getDutyDictionaryId(), linkman.getLinkmanDuty()));
    boolean result = false;

    result = this.linkmanDAO.updateLinkmanByStaticParam(linkman);

    Linkman linkman2 = new Linkman();
    linkman2 = this.linkmanDAO.getLinkman(linkman.getId());
    if (linkman2 != null) {
      linkman2.setLinkmanId(linkman2.getId());
      linkman2.setUpdatePersonId(linkman.getCurrentUserId());
      linkman2.setUpdateDate(new Date());
      linkman2.setIsPrimitive("0");
      this.linkmanUpdateHistoryDAO.insertLinkmanUpdateHistory(linkman2);
    }

    return result;
  }

  public boolean deleteLinkman(Integer id) {
    if (id == null) return false;
    Linkman linkman = new Linkman();
    linkman.setId(id);
    linkman.setStatus("1");
    return this.linkmanDAO.updateLinkman(linkman);
  }

  public List getDeptCustomerLinkMan(Integer userid, Page page, String customerName, QueryLinkman queryLinkman) {
    Linkman linkman = new Linkman();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      linkman.setCustomerName(customerName);
    }

    if (queryLinkman != null) {
      if (!StringUtils.isEmpty(queryLinkman.getCustomerName())) {
        linkman.setCustomerName(queryLinkman.getCustomerName());
      }
      if (!StringUtils.isEmpty(queryLinkman.getLinkmanName())) {
        linkman.setLinkmanName(queryLinkman.getLinkmanName());
      }
      if (queryLinkman.getAreaId() != null) {
        linkman.setAreaId(queryLinkman.getAreaId());
      }
      if (queryLinkman.getIndustryId() != null) {
        linkman.setIndustryId(queryLinkman.getIndustryId());
      }
      if (!StringUtils.isEmpty(queryLinkman.getOfficePhone())) {
        linkman.setOfficePhone(queryLinkman.getOfficePhone());
      }
      if (!StringUtils.isEmpty(queryLinkman.getMobile())) {
        linkman.setMobile(queryLinkman.getMobile());
      }
      if (!StringUtils.isEmpty(queryLinkman.getEmail())) {
        linkman.setEmail(queryLinkman.getEmail());
      }
    }
    String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
    linkman.setSalePersons(salePersonIds);
    linkman.setPageStartRow(page.getPageStartRow());
    linkman.setPageEndRow(page.getPageEndRow());
    return this.linkmanDAO.getDeptCustomerLinkMan(linkman);
  }

  public Integer getDeptCustomerLinkManCount(Integer userid, String customerName, QueryLinkman queryLinkman) {
    Linkman linkman = new Linkman();
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      linkman.setCustomerName(customerName);
    }

    if (queryLinkman != null) {
      if (!StringUtils.isEmpty(queryLinkman.getCustomerName())) {
        linkman.setCustomerName(queryLinkman.getCustomerName());
      }
      if (!StringUtils.isEmpty(queryLinkman.getLinkmanName())) {
        linkman.setLinkmanName(queryLinkman.getLinkmanName());
      }
      if (queryLinkman.getAreaId() != null) {
        linkman.setAreaId(queryLinkman.getAreaId());
      }
      if (queryLinkman.getIndustryId() != null) {
        linkman.setIndustryId(queryLinkman.getIndustryId());
      }
      if (!StringUtils.isEmpty(queryLinkman.getOfficePhone())) {
        linkman.setOfficePhone(queryLinkman.getOfficePhone());
      }
      if (!StringUtils.isEmpty(queryLinkman.getMobile())) {
        linkman.setMobile(queryLinkman.getMobile());
      }
      if (!StringUtils.isEmpty(queryLinkman.getEmail())) {
        linkman.setEmail(queryLinkman.getEmail());
      }
    }
    String salePersonIds = this.departmentService.getCurrentDeptUserIdAndSubDeptUserIdsByUserId(userid);
    linkman.setSalePersons(salePersonIds);
    return this.linkmanDAO.getDeptCustomerLinkManCount(linkman);
  }
  public List getOwnCustomerLinkMan(Integer userid, Page page, String customerName, QueryLinkman queryLinkman) {
    Linkman linkman = new Linkman();
    linkman.setSalePersonId(userid);
    linkman.setPageStartRow(page.getPageStartRow());
    linkman.setPageEndRow(page.getPageEndRow());
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      linkman.setCustomerName(customerName);
    }

    if (queryLinkman != null) {
      if (!StringUtils.isEmpty(queryLinkman.getCustomerName())) {
        linkman.setCustomerName(queryLinkman.getCustomerName());
      }
      if (!StringUtils.isEmpty(queryLinkman.getLinkmanName())) {
        linkman.setLinkmanName(queryLinkman.getLinkmanName());
      }
      if (queryLinkman.getAreaId() != null) {
        linkman.setAreaId(queryLinkman.getAreaId());
      }
      if (queryLinkman.getIndustryId() != null) {
        linkman.setIndustryId(queryLinkman.getIndustryId());
      }
      if (!StringUtils.isEmpty(queryLinkman.getOfficePhone())) {
        linkman.setOfficePhone(queryLinkman.getOfficePhone());
      }
      if (!StringUtils.isEmpty(queryLinkman.getMobile())) {
        linkman.setMobile(queryLinkman.getMobile());
      }
      if (!StringUtils.isEmpty(queryLinkman.getEmail())) {
        linkman.setEmail(queryLinkman.getEmail());
      }
    }
    return this.linkmanDAO.getDeptCustomerLinkMan(linkman);
  }

  public Integer getOwnCustomerLinkManCount(Integer userid, String customerName, QueryLinkman queryLinkman) {
    Linkman linkman = new Linkman();
    linkman.setSalePersonId(userid);
    if ((!StringUtils.isEmpty(customerName)) && 
      (customerName != "客户名称")) {
      linkman.setCustomerName(customerName);
    }

    if (queryLinkman != null) {
      if (!StringUtils.isEmpty(queryLinkman.getCustomerName())) {
        linkman.setCustomerName(queryLinkman.getCustomerName());
      }
      if (!StringUtils.isEmpty(queryLinkman.getLinkmanName())) {
        linkman.setLinkmanName(queryLinkman.getLinkmanName());
      }
      if (queryLinkman.getAreaId() != null) {
        linkman.setAreaId(queryLinkman.getAreaId());
      }
      if (queryLinkman.getIndustryId() != null) {
        linkman.setIndustryId(queryLinkman.getIndustryId());
      }
      if (!StringUtils.isEmpty(queryLinkman.getOfficePhone())) {
        linkman.setOfficePhone(queryLinkman.getOfficePhone());
      }
      if (!StringUtils.isEmpty(queryLinkman.getMobile())) {
        linkman.setMobile(queryLinkman.getMobile());
      }
      if (!StringUtils.isEmpty(queryLinkman.getEmail())) {
        linkman.setEmail(queryLinkman.getEmail());
      }
    }
    return this.linkmanDAO.getDeptCustomerLinkManCount(linkman);
  }
}